package com.book.lizhi.database;

import android.support.v4.util.LruCache;
import android.text.TextUtils;

import com.book.lizhi.util.FileUtil;

public class JsonCache {
	private LruCache<String, String> mCache;

	public JsonCache() {
		int maxSize = 10 * 1024 * 1024;
		mCache = new LruCache<String, String>(maxSize) {

			@Override
			protected int sizeOf(String key, String value) {
				if (!TextUtils.isEmpty(value)) {
					return value.getBytes().length;
				}
				return 0;
			}

		};
	}

	public String getJsonStr(String url) {
		if (mCache.get(url) == null) {
			return FileUtil.getStringByKey(createKey(url));
		}
		return mCache.get(url);
	}

	public void saveJsonStr(String url, String json) {
		mCache.put(url, json);
		FileUtil.saveStringByKey(createKey(url), json);

	}

	private String createKey(String url) {
		return String.valueOf(url.hashCode());
	}
}
